﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for PhanLoaiCtr
/// </summary>
public class PhanLoaiCtr
{
    SqlDataAdapter da;
    DataSet ds;
    DataProvider con = new DataProvider();
	public PhanLoaiCtr()
	{
		
	}
    
    public DataTable layPhanLoai()
    {
        try
        {
            string sql = "select * from PhanLoai Where TrangThai='true'";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable layPhanLoaiTheoMaDM(int MaDM)
    {
        try
        {
            DataTable bangPhanLoai = DataProvider.GetData("Select * from PhanLoai where MaDanhMuc=@MaDM and TrangThai='True'", "@MaDM", MaDM);
            return bangPhanLoai;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public EPhanLoai layPhanLoai(string LoaiSP)
    {
        try
        {
            DataTable tablePhanLoai = DataProvider.GetData("Select * from PhanLoai Where LoaiSP=@LoaiSP and TrangThai='True'","@LoaiSP",LoaiSP);
            EPhanLoai PhanLoai = new EPhanLoai();
            PhanLoai.LoaiSP = Int32.Parse(tablePhanLoai.Rows[0]["LoaiSP"].ToString());
            PhanLoai.TenLoaiSP = tablePhanLoai.Rows[0]["TenLoaiSP"].ToString();
            PhanLoai.MaDanhMuc = Int32.Parse(tablePhanLoai.Rows[0]["MaDanhMuc"].ToString());
            return PhanLoai;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
    public bool InsertPhanLoai(EPhanLoai PhanLoai)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO PhanLoai VAlUES(@LoaiSP,@TenLoaiSP,@MaDanhMuc)",
                "@LoaiSP", PhanLoai.LoaiSP, "@TenLoaiSP", PhanLoai.TenLoaiSP, "@MaDanhMuc", PhanLoai.MaDanhMuc);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeletePhanLoai(int LoaiSP)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM PhanLoai WHERE LoaiSP=@LoaiSP","@LoaiSP",LoaiSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool UpdatePhanLoai(int LoaiSP,string TenLoaiSP)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update PhanLoai set TenLoaiSP=@TenLoaiSP WHERE LoaiSP=@LoaiSP", "@LoaiSP", LoaiSP,"@TenLoaiSP",TenLoaiSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeletePhanLoai1(int LoaiSP)
    {
        try
        {
            DataProvider.ExecuteNonQuery("exec Xoa_Phan_Loai @LoaiSP", "@LoaiSP", LoaiSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable GetMaPhanLoai()
    {
        try
        {
            return DataProvider.GetData("Select MAx(LoaiSP) as LoaiSP from PhanLoai");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //Hanh
    public DataTable layPhanLoaiH()
    {
        try
        {
            string sql = "select *,TenDanhMuc from PhanLoai,DanhMuc where danhmuc.madanhmuc=phanloai.madanhmuc and phanloai.TrangThai=1";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public int demLoaiSP()
    {
        String sql = "Select count(*) from PhanLoai where TrangThai=1";
        da = new SqlDataAdapter(sql, DataProvider.GetConnection());
        ds = new DataSet();
        da.Fill(ds);
        return int.Parse(ds.Tables[0].Rows[0][0].ToString());

    }
    public bool InsertPhanLoaiH(EPhanLoai PhanLoai)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO PhanLoai VAlUES(@TenLoaiSP,@MaDanhMuc,@TrangThai)",
                "@TenLoaiSP", PhanLoai.TenLoaiSP, "@MaDanhMuc", PhanLoai.MaDanhMuc, "@TrangThai", true);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeletePhanLoaiH(int LoaiSP)
    {
        try
        {
            //DataProvider.ExecuteNonQuery("DELETE FROM PhanLoai WHERE LoaiSP=@LoaiSP","@LoaiSP",LoaiSP);
            DataProvider.ExecuteNonQuery("Update PhanLoai set TrangThai=0 WHERE LoaiSP=@LoaiSP", "@LoaiSP", LoaiSP);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool UpdatePhanLoaiH(int LoaiSP, string TenLoaiSP,string maDanhMuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update PhanLoai set TenLoaiSP=@TenLoaiSP,MaDanhMuc=@MaDanhMuc WHERE LoaiSP=@LoaiSP", "@LoaiSP", LoaiSP, "@TenLoaiSP", TenLoaiSP,"@MaDanhMuc",maDanhMuc);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public EPhanLoai layPhanLoaiH(string LoaiSP)
    {
        try
        {
            DataTable tablePhanLoai = DataProvider.GetData("Select * from PhanLoai Where LoaiSP=@LoaiSP", "@LoaiSP", LoaiSP);
            EPhanLoai PhanLoai = new EPhanLoai();
            PhanLoai.LoaiSP = Int32.Parse(tablePhanLoai.Rows[0]["LoaiSP"].ToString());
            PhanLoai.TenLoaiSP = tablePhanLoai.Rows[0]["TenLoaiSP"].ToString();
            PhanLoai.MaDanhMuc = Int32.Parse(tablePhanLoai.Rows[0]["MaDanhMuc"].ToString());
            return PhanLoai;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}